#!/bin/bash

# 安装库的函数
# 参数1：库名称
# 参数2：Git标签
install_library() {
    local lib_name=$1
    local git_tag=$2

    echo "Installing ${lib_name}..."
    cd "${lib_name}" || exit
    git checkout "${git_tag}"
    mkdir -p my_build && cd my_build || exit
    cmake .. -DCMAKE_INSTALL_PREFIX=../../linux_install/${lib_name} || exit
    make -j"${BUILD_THREADS}" || exit
    sudo make install || exit
    cd ../.. || exit
    rm -rf "${lib_name}"
}

# 主程序开始
echo "Initializing git submodules..."
git clone https://gitee.com/LauZanMo/glog.git
git clone https://gitee.com/jiajiduan/googletest.git
git clone https://gitee.com/forests/gflags.git
git clone https://gitee.com/switchos/jsoncpp.git

# 可以通过命令行参数指定构建线程数，默认为32
BUILD_THREADS=${1:-32}

# 安装各个库
install_library glog v0.5.0
install_library googletest v1.14.0
install_library gflags v2.2.2
install_library jsoncpp 1.9.5

echo "All libraries installed successfully."
